package com.sytc.camera.uicontroller;


import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;



@Component
public class MainuiController {

    private static final Logger log = LoggerFactory.getLogger(MainuiController.class);


    @FXML
    SplitPane rootPane;
    @FXML
    AnchorPane headPane;
    @FXML
    HBox hbox;
    @FXML
    Label logo;
    @FXML
    Label title;
    @FXML
    Label min;
    @FXML
    Label max;
    @FXML
    Label close;
    private double originalWidth;
    private double originalHeight;
    private double originalX;
    private double originalY;

    @FXML
    public void initialize() {

        // 设置样式
        setStyle();

        // 设置为可改变大小
        changeSize();

        // 设置为可移动
        move();

    }

    public void onMin(MouseEvent mouseEvent) {
        Stage stage = (Stage) rootPane.getScene().getWindow();
        stage.setIconified(true);
    }

    public void onClose(MouseEvent mouseEvent) {
        Stage stage = (Stage) rootPane.getScene().getWindow();
        stage.close();
    }

    public void onMax(MouseEvent mouseEvent) {
        Stage stage = (Stage) rootPane.getScene().getWindow();
        // 检查窗口是否已经最大化
        if (stage.isMaximized()) {
            // 如果窗口已经最大化，则恢复原始大小和位置
            stage.setMaximized(false);
            stage.setWidth(originalWidth);
            stage.setHeight(originalHeight);
            stage.setX(originalX);
            stage.setY(originalY);
            max.getStyleClass().remove("max-min");
            max.getStyleClass().add("max");
        } else {
            // 如果窗口没有最大化，保存当前窗口大小和位置，并最大化窗口
            originalWidth = stage.getWidth();
            originalHeight = stage.getHeight();
            originalX = stage.getX();
            originalY = stage.getY();
            stage.setMaximized(true);
            max.getStyleClass().remove("max");
            max.getStyleClass().add("max-min");
        }
    }


    private void setStyle() {
        /* 设置标题栏样式 */
        // 添加样式表,加载后自己和子类都可以用
        rootPane.getStylesheets().add(this.getClass().getResource("/ui/css/head.css").toExternalForm());
        logo.getStyleClass().add("logo");
        logo.setText("");
        title.getStyleClass().add("title");
        title.setText("乐学小屋");
        min.getStyleClass().add("min");
        min.setText("");
        max.getStyleClass().add("max");
        max.setText("");
        close.getStyleClass().add("close");
        close.setText("");
    }

    private void move() {
        // 创建一个数组来共享数据
        double[] mouseX = new double[1];  // 存储鼠标 X 坐标
        double[] mouseY = new double[1];  // 存储鼠标 Y 坐标
        Point2D[] panePoint = new Point2D[1];  // 存储 rootPane 的位置

        // 鼠标按下事件
        headPane.setOnMousePressed(event -> {
            mouseX[0] = event.getScreenX();  // 设置鼠标按下时的屏幕坐标
            mouseY[0] = event.getScreenY();
            panePoint[0] = rootPane.localToScreen(rootPane.getLayoutX(), rootPane.getLayoutY()); // 获取 rootPane 的位置
        });

        // 鼠标拖动事件
        headPane.setOnMouseDragged(event -> {
            Stage stage = (Stage) rootPane.getScene().getWindow();

            // 计算窗口的新位置
            stage.setX(event.getScreenX() - mouseX[0] + panePoint[0].getX());
            stage.setY(event.getScreenY() - mouseY[0] + panePoint[0].getY());
        });
    }

    private void changeSize() {
        // 鼠标移动到边缘时变为拖拽样式
        rootPane.setOnMouseMoved(event -> {
            double width = rootPane.getWidth();
            double height = rootPane.getHeight();
            double mouseX = event.getScreenX();
            double mouseY = event.getScreenY();
            Point2D panePoint = rootPane.localToScreen(rootPane.getLayoutX(), rootPane.getLayoutY());

            // 判断下边缘
            if (mouseY >= panePoint.getY() + height - 2 && mouseY <= panePoint.getY() + rootPane.getHeight()) {
                // 右下角
                if (mouseX >= panePoint.getX() + width - 5) {
                    rootPane.setCursor(Cursor.SE_RESIZE); // 右下角拖动
                } else {
                    rootPane.setCursor(Cursor.S_RESIZE); // 下边拖动
                }
            }
            // 判断右边缘
            else if (mouseX >= panePoint.getX() + width - 2 && mouseX <= panePoint.getX() + rootPane.getWidth()) {
                rootPane.setCursor(Cursor.E_RESIZE); // 右边拖动
            } else {
                rootPane.setCursor(Cursor.DEFAULT); // 恢复默认光标
            }
        });

        // 鼠标拖动事件
        rootPane.setOnMouseDragged(event -> {
            // 如果鼠标不是默认，不处理
            if(rootPane.getCursor() == Cursor.DEFAULT)
                return;

            Stage stage = (Stage) rootPane.getScene().getWindow();
            Point2D panePoint = rootPane.localToScreen(rootPane.getLayoutX(), rootPane.getLayoutY());

            /* 根据不同的鼠标光标调整窗口大小 */
            // 右边拖动
            if (rootPane.getCursor() == Cursor.E_RESIZE) {
                stage.setWidth(event.getScreenX() - panePoint.getX());
            }
            // 下边拖动
            else if (rootPane.getCursor() == Cursor.S_RESIZE) {
                stage.setHeight(event.getScreenY() - panePoint.getY());
            }
            // 右下角拖动
            else if (rootPane.getCursor() == Cursor.SE_RESIZE) {
                stage.setWidth(event.getScreenX() - panePoint.getX());
                stage.setHeight(event.getScreenY() - panePoint.getY());
            }
        });
    }

}
